package com.qf.controller;

import com.qf.dao.CartDaoImpl;
import com.qf.pojo.Cart;
import com.qf.pojo.User;
import com.qf.service.CartService;
import com.qf.service.CartServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author 汤奇
 * @version V1.0
 * @Project BookPro
 * @Package com.qf.controller
 * @Description: 购物车管理
 * @Date 2022/5/25 17:25
 */
@WebServlet("/cart")
public class CartController extends BaseServlet{
    CartService cartService = new CartServiceImpl(new CartDaoImpl());
    /**
     * 创建购物车
     * */
    public String create(HttpServletRequest request, HttpServletResponse response){
        //1.获取商品id
        String pid = request.getParameter("pid");
        //2.获取session会话中当前用户的用户id
        User user = (User) request.getSession().getAttribute("loginUser");
        //判断用户是否登录
        if (user == null){
            //用户没登录,跳转到登录页面
            return "redirect:/login.jsp";
        }
        //3.调用service业务层, 将购物车存入到数据库中
        cartService.createCart(user.getId(),Integer.parseInt(pid));
        //4.跳转到购物车查询列表方法
        return "redirect:/cart?method=show";
    }

    /**
     * 查询指定用户的购物车数据, 并跳转到购物车列表页面展示
     * */
    public String show(HttpServletRequest request, HttpServletResponse response){
        //1.获取session会话中当前用户的id
        User user = (User) request.getSession().getAttribute("loginUser");
        if (user == null){
            //用户未登录, 重定向到登录页面
            return "redirect:/login.jsp";
        }
        //2.根据用户id调用service查询这个用户的购物车数据
        List<Cart> list = cartService.findCartListByUid(user.getId());
        //3.将用户购物车的数据放入request域中, 带到页面中进行展示
        request.setAttribute("list",list);
        //4.跳转到购物车列表页面展示
        return "forward:/cart.jsp";
    }

    /**
     * 修改购物车中商品购买的数量
     * */
    public String update(HttpServletRequest request, HttpServletResponse response){
        //1.从会话中获取当前登录用户的id, 如果获取不到跳转到登录页面
        User user = (User) request.getSession().getAttribute("loginUser");
        if (user == null){
            //没登录,跳转到登录页
            return "redirect:/login.jsp";
        }
        //2.获取购物车的主键id
        String  cid = request.getParameter("cid");
        //获取购买数量
        String cnum = request.getParameter("cnum");
        //3.调用service, 执行修改业务
        cartService.updateCart(Integer.parseInt(cid),Integer.parseInt(cnum));
        //4.跳转到购物车列表, 查看购物车中最新的数据展示
        return "redirect:/cart?method=show";
    }

    /**
     * 删除购物车中的一条商品
     * */
    public String delete(HttpServletRequest request, HttpServletResponse response){
        //1.从session会话中获取当前登录用户的id
        User user = (User) request.getSession().getAttribute("loginUser");
        if (user == null){
            return "redirect:/login.jsp";
        }
        //2.获取购物项主键id
        String cid = request.getParameter("cid");
        //3.调用service方法,根据购物项主键id执行删除业务
        cartService.deleteById(Integer.parseInt(cid));
        //4.跳转到购物车列表, 查看购物车中最新的数据展示
        return "redirect:/cart?method=show";
    }

    /**
     * 清空购物车
     * */
    public String clear(HttpServletRequest request, HttpServletResponse response){
        //1.从session会话中获取当前登录用户的信息
        User user = (User) request.getSession().getAttribute("loginUser");
        //2.获取用户id
//        String uid = request.getParameter("uid");
        //3.根据用户id清空购物车
        cartService.clearCart(user.getId());
        //4.跳转到购物车列表, 查看购物车中最新的数据展示
        return "redirect:/cart?method=show";
    }
}
